﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChequeManagement.PL
{
    public partial class BankEntity : PL.Base.Base
    {
        public BankEntity()
        {
            InitializeComponent();
        }
        //This Is a sample text.
        private void AddNewBankButton_Click(object sender, EventArgs e)
        {
            BLL.BLLBankFactory bankFactory = new BLL.BLLBankFactory();

            try
            {
                BLL.BLLBank bankObject = new BLL.BLLBank();

                bankObject.BankName = BankNameTextBox.Text;
                bankFactory.BeginTransaction();
                bankFactory.Add(bankObject);
                bankFactory.CommitTransaction();
                LoadBanks();
            }
            catch (Exception ex)
            {
                bankFactory.RollBackTransaction();
                MessageBox.Show(ex.Message);
            }
        }

        private void EditBankButton_Click(object sender, EventArgs e)
        {
            if (AllBanksComboBox.SelectedIndex != -1)
            {
                if (BankNameTextBox.Text != "")
                {
                    BLL.BLLBankFactory bankFactory = new BLL.BLLBankFactory();

                    try
                    {
                        int ID = (int)AllBanksComboBox.SelectedValue;
                        BLL.BLLBank bankObject = new BLL.BLLBank();

                        bankObject.BankName = BankNameTextBox.Text;
                        bankFactory.BeginTransaction();
                        bankFactory.Edit(bankObject, ID);
                        bankFactory.CommitTransaction();
                        LoadBanks();
                    }
                    catch (Exception ex)
                    {
                        bankFactory.RollBackTransaction();
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("لطفا نام بانک را وارد نمایید");
                }
            }
            else
            {
                MessageBox.Show("لطفا یک بانک را انتخاب کنید");
            }
        }

        private void Save()
        {
            BLL.BLLBankFactory bankFactory = new BLL.BLLBankFactory();

            try
            {
                BLL.BLLBank bankObject = new BLL.BLLBank();

                bankObject.BankName = this.BankNameTextBox.Text;
                bankFactory.BeginTransaction();
                bankFactory.Add(bankObject);
                bankFactory.CommitTransaction();
            }
            catch (Exception ex)
            {
                bankFactory.RollBackTransaction();
                MessageBox.Show(ex.Message);
            }
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ClearForm()
        {
            BankNameTextBox.Text = "";
        }

        private void BankEntity_Load(object sender, EventArgs e)
        {
            LoadBanks();
        }

        private void LoadBanks()
        {
            BLL.BLLBankFactory bllBankObject = new BLL.BLLBankFactory();
            DataTable dtAllbanks = new DataTable();
            bllBankObject.GetAll(dtAllbanks);
            AllBanksComboBox.DataSource = dtAllbanks;
            AllBanksComboBox.DisplayMember = "BankName";
            AllBanksComboBox.ValueMember = "BankID";
            BankNameTextBox.Text = "";
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            if (AllBanksComboBox.SelectedIndex >= 0)
            {
                BLL.BLLBankFactory bankFactory = new BLL.BLLBankFactory();

                try
                {
                    int ID = (int)AllBanksComboBox.SelectedValue;
                    bankFactory.BeginTransaction();
                    bankFactory.Remove(ID); ;
                    bankFactory.CommitTransaction();
                    LoadBanks();
                }
                catch (Exception ex)
                {
                    bankFactory.RollBackTransaction();
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("لطفا یک بانک را انتخاب کنید");
            }
        }

        private void AllBanksComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            BankNameTextBox.Text = AllBanksComboBox.Text;
        }


    }
}
